/*
 * @Date: 2025-07-19 01:12:21
 * @LastEditors: 贾二小 erxiao.jia@outlook.com
 * @LastEditTime: 2025-11-17 00:28:45
 * @FilePath: /admin-vue/src/plugins/pinia/index.ts
 */
import type { App } from 'vue'
import { createPinia, type PiniaPluginContext } from 'pinia'

const setup = (app: App) => {
  const pinia = createPinia()
 
  pinia.use(piniaPlugin)
  app.use(pinia)
}

const piniaPlugin = (context: PiniaPluginContext) => {
  const { store } = context
  const KEY = 'PINIA_' + store.$id
  //存
  window.addEventListener('beforeunload', () => {
    localStorage.setItem(KEY, JSON.stringify(store.$state))
  })

  //取
  const item = localStorage.getItem(KEY)
  if (!item) {
    return
  }

  try {
    const originState = JSON.parse(item)
    // store.$patch(originState)
  } catch (error) {
    console.log(error)
  }
}

export { setup }
